import javax.swing.*;
import java.awt.*;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;

public class FocusDemo extends JFrame implements FocusListener {

   private JTextArea textArea;

   public FocusDemo() {
       setTitle("JTextArea Focus Demo");
       setSize(400, 200);
       setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

       Container container = getContentPane();
       container.setLayout(new FlowLayout());

       textArea = new JTextArea();
       textArea.setColumns(20);
       container.add(textArea);

       textArea.addFocusListener(this);

       setVisible(true);
   }

   @Override
   public void focusGained(FocusEvent e) {
       System.out.println("Focus gained: " + e.getSource());
       textArea.setEditable(true);
   }

   @Override
   public void focusLost(FocusEvent e) {
       System.out.println("Focus lost: " + e.getSource());
       textArea.setEditable(false);
   }

   public static void main(String[] args) {
       SwingUtilities.invokeLater(new Runnable() {
           @Override
           public void run() {
               new FocusDemo();
           }
       });
   }
}